#! /bin/sh
# chkconfig: 2345 11 83
# description: Start/stop/restart lustre and show the lustre status(used for login farm)
# Modified by wanglu
# Modified date: 2019-1-18
# nms check parameter: besfs bes3fs besfs3 dybfs junofs workfs scratchfs cefs publicfs sharefs dyfs2 hpcfs

check_rst(){
        if [ $? -ne 0 ];then
                printf "\t $1 [\033[31m Failed"
                printf "\033[0m ]"
        else
                 printf "\t $1 [\033[32m OK"
                 printf "\033[0m ]"
        fi
        return 0
}
#verify fsnmae whether to mount
is_mount() {
        if [ $# -lt 1 ] ;then 
                echo "Invalid Argument"
                return 1 
        fi
        local fsname=$1
        rst=$(/bin/mount  -l -t lustre|grep "$fsname ")
        if [ "x$rst" = "x" ];then
                echo "NO"
        else
                #echo -e "$fsname is already mounted"
                echo "YES" 
        fi
        return 0
}
#verify fsnmae whether to umount
is_umount() {
        if [ $# -lt 1 ] ;then
                echo "Invalid Argument"
                return 1
        fi
        local fsname=$1
        rst=$(/bin/mount  -l -t lustre|grep "$fsname ")
        if [ "x$rst" = "x" ];then
                #echo -e "$fsname is already umounted"
                echo "YES"
        else
                echo "NO"
        fi
        return 0
}
   
#mod_lmem(){
#        set +f
#        lmem=4096
        #res=$((`free | grep Mem | awk '{print $2}'`/64000))
        #if [ $res -gt $lmem ];then
        #        lmem=$res
        #fi
        #echo $lmem > /proc/fs/lustre/llite/*/max_cached_mb
#        /usr/sbin/lctl set_param llite.*.max_cached_mb=$lmem >/dev/null 2>&1
#}
mylog=/var/log/mymount
echo `date` >> $mylog
/sbin/modprobe lustre >> $mylog 2>&1
if [ $? -ne 0 ];then
        echo "modprobe lustre Failed"
        exit 1
fi
start() {
        # Stop nfslock service to avoid port 988 is already used
        /sbin/service nfslock stop
        echo -n "Starting lustre:"
        mkdir -p /besfs
        IM=$(is_mount besfs)
        if [ "$IM" = "NO" ];then
              /bin/mount -t lustre  192.168.50.181:/besfs /besfs >> $mylog 2>&1
        fi
        check_rst besfs
        mkdir -p /bes3fs
        IM=$(is_mount bes3fs)
        if [ "$IM" = "NO" ];then
              /bin/mount -t lustre  192.168.50.159:/bes3fs /bes3fs >> $mylog 2>&1
        fi
        check_rst bes3fs
        mkdir -p /besfs3
        IM=$(is_mount besfs3)
        if [ "$IM" = "NO" ];then
              /bin/mount -t lustre  192.168.50.230:/bprofs /besfs3 >> $mylog 2>&1
        fi
        check_rst besfs3
        mkdir -p /dybfs
        IM=$(is_mount dybfs)
        if [ "$IM" = "NO" ];then
              /bin/mount -t lustre  192.168.50.149:/dybfs /dybfs >> $mylog 2>&1
        fi
	check_rst dybfs
        mkdir -p /workfs
        IM=$(is_mount workfs)
        if [ "$IM" = "NO" ];then
              /bin/mount -t lustre -o ro 192.168.50.138:/workfs /workfs >> $mylog 2>&1
        fi
        check_rst workfs
        mkdir -p /scratchfs
        IM=$(is_mount scratchfs)
        if [ "$IM" = "NO" ];then
              /bin/mount -t lustre  192.168.50.55:/scrachfs /scratchfs >> $mylog 2>&1
        fi
        check_rst scratchfs
        mkdir -p /junofs
        IM=$(is_mount junofs)
        if [ "$IM" = "NO" ];then
              /bin/mount -t lustre  192.168.50.200:/junofs /junofs >> $mylog 2>&1
        fi
        check_rst junofs
        mkdir -p /cefs
        IM=$(is_mount cefs)
        if [ "$IM" = "NO" ];then
              /bin/mount -t lustre   192.168.50.114:/cefs /cefs >> $mylog 2>&1
        fi
        check_rst cefs
        mkdir -p /publicfs
        IM=$(is_mount publicfs)
        if [ "$IM" = "NO" ];then
              /bin/mount -t lustre  192.168.50.63:/acfs /publicfs >> $mylog 2>&1
        fi
        check_rst publicfs
        mkdir -p /sharefs
        IM=$(is_mount sharefs)
        if [ "$IM" = "NO" ];then
              /bin/mount -t lustre  192.168.50.77:/sharefs /sharefs >> $mylog 2>&1
        fi
        check_rst sharefs
	mkdir -p /dybfs2
        IM=$(is_mount dybfs2)
        if [ "$IM" = "NO" ];then
              /bin/mount -t lustre  192.168.50.23:/dybfs2 /dybfs2 >> $mylog 2>&1
        fi
        check_rst dybfs2
	mkdir -p /hpcfs
        IM=$(is_mount hpcfs)
        if [ "$IM" = "NO" ];then
              /bin/mount -t lustre  -o flock 192.168.50.33:/hpcfs /hpcfs >> $mylog 2>&1
        fi
        check_rst hpcfs

        echo ""
#        mod_lmem
        /sbin/service nfslock start
	#/usr/sbin/lctl set_param timeout=1000 >/dev/null 2>&1
	#/usr/sbin/lctl set_param ldlm_timeout=40 >/dev/null 2>&1
}


stop() {
       echo -n "Stopping lustre:"
       IM=$(is_umount besfs)
       if [ "$IM" = "NO" ];then
            /bin/umount -l /besfs >> $mylog 2>&1
       fi
       check_rst besfs
       IM=$(is_umount bes3fs)
       if [ "$IM" = "NO" ];then
            /bin/umount -l /bes3fs >> $mylog 2>&1
       fi
       check_rst bes3fs
       IM=$(is_umount besfs3)
       if [ "$IM" = "NO" ];then
            /bin/umount -l /besfs3 >> $mylog 2>&1
       fi
       check_rst besfs3
       IM=$(is_umount publicfs)
       if [ "$IM" = "NO" ];then
            /bin/umount -l /publicfs >> $mylog 2>&1
       fi
       check_rst publicfs
       IM=$(is_umount sharefs)
       if [ "$IM" = "NO" ];then
            /bin/umount -l /sharefs >> $mylog 2>&1
       fi
       check_rst sharefs
       IM=$(is_umount dybfs)
       if [ "$IM" = "NO" ];then
            /bin/umount -l /dybfs >> $mylog 2>&1
       fi
       check_rst dybfs
       IM=$(is_umount workfs)
       if [ "$IM" = "NO" ];then
            /bin/umount -l /workfs >> $mylog 2>&1
       fi
       check_rst workfs
       IM=$(is_umount scratchfs)
       if [ "$IM" = "NO" ];then
            /bin/umount -l /scratchfs >> $mylog 2>&1
       fi
       check_rst scratchfs
       IM=$(is_umount junofs)
       if [ "$IM" = "NO" ];then
            /bin/umount -l /junofs >> $mylog 2>&1
       fi
       check_rst junofs
       IM=$(is_umount cefs)
       if [ "$IM" = "NO" ];then
            /bin/umount -l /cefs >> $mylog 2>&1
       fi
       check_rst cefs
       IM=$(is_umount hpcfs)
       if [ "$IM" = "NO" ];then
            /bin/umount -l /hpcfs >> $mylog 2>&1
       fi
       check_rst hpcfs
       IM=$(is_umount dybfs2)
       if [ "$IM" = "NO" ];then
            /bin/umount -l /dybfs2 >> $mylog 2>&1
       fi
       check_rst dybfs2


       echo ""
#       mod_lmem

}
status() {
        rst1=$(/bin/mount  -l -t lustre|grep "bes3fs ")
        rst2=$(/bin/mount  -l -t lustre|grep "besfs ")
        rst3=$(/bin/mount  -l -t lustre|grep "publicfs ")
        rst4=$(/bin/mount  -l -t lustre|grep "sharefs ")
        rst5=$(/bin/mount  -l -t lustre|grep "dybfs ")
        rst6=$(/bin/mount  -l -t lustre|grep "workfs ")
        rst7=$(/bin/mount  -l -t lustre|grep "scratchfs ")
        rst8=$(/bin/mount  -l -t lustre|grep "junofs ")
        rst9=$(/bin/mount  -l -t lustre|grep "cefs ")
        rst10=$(/bin/mount  -l -t lustre|grep "besfs3 ")
        rst11=$(/bin/mount  -l -t lustre|grep "dybfs2 ")
        rst12=$(/bin/mount  -l -t lustre|grep "hpcfs ")
        if [ "x$rst1" = "x" ];then
                echo "Service lustre is stopped..."
        elif [ "x$rst2" = "x" ];then
                echo "Service lustre is stopped..."
        elif [ "x$rst3" = "x" ];then
                echo "Service lustre is stopped..."
        elif [ "x$rst4" = "x" ];then
                echo "Service lustre is stopped..."
        elif [ "x$rst5" = "x" ];then
                echo "Service lustre is stopped..."
        elif [ "x$rst6" = "x" ];then
                echo "Service lustre is stopped..."
        elif [ "x$rst7" = "x" ];then
                echo "Service lustre is stopped..."
        elif [ "x$rst8" = "x" ];then
                echo "Service lustre is stopped..."
        elif [ "x$rst9" = "x" ];then
                echo "Service lustre is stopped..."
        elif [ "x$rst10" = "x" ];then
                echo "Service lustre is stopped..."
	elif [ "x$rst11" = "x" ];then
                echo "Service lustre is stopped..."
	elif [ "x$rst12" = "x" ];then
                echo "Service lustre is stopped..."
        else
                echo "service lustre is running..."
        
        fi
}

restart() {
	stop
	start
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status
	;;
  restart|reload)
	restart
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|reload}"
	exit 1
esac

exit $?
